<?php

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\model\Category;

class InitCategories extends Command
{
    protected function configure()
    {
        $this->setName('init:categories')
            ->setDescription('Initialize system categories');
    }

    protected function execute(Input $input, Output $output)
    {
        try {
            // 插入系统分类
            $systemCategories = [
                [
                    'user_id' => null,
                    'name' => '开发工具',
                    'icon' => '💻',
                    'color' => '#1890ff',
                    'description' => '编程开发相关的账号',
                    'sort' => 1,
                    'is_system' => 1,
                    'is_active' => 1,
                ],
                [
                    'user_id' => null,
                    'name' => '社交媒体',
                    'icon' => '📱',
                    'color' => '#52c41a',
                    'description' => '社交平台和媒体账号',
                    'sort' => 2,
                    'is_system' => 1,
                    'is_active' => 1,
                ],
                [
                    'user_id' => null,
                    'name' => '云服务',
                    'icon' => '☁️',
                    'color' => '#722ed1',
                    'description' => '云计算和网络服务',
                    'sort' => 3,
                    'is_system' => 1,
                    'is_active' => 1,
                ],
                [
                    'user_id' => null,
                    'name' => '金融理财',
                    'icon' => '💰',
                    'color' => '#fa8c16',
                    'description' => '银行、支付、投资理财',
                    'sort' => 4,
                    'is_system' => 1,
                    'is_active' => 1,
                ],
                [
                    'user_id' => null,
                    'name' => '娱乐游戏',
                    'icon' => '🎮',
                    'color' => '#eb2f96',
                    'description' => '游戏、视频、音乐娱乐',
                    'sort' => 5,
                    'is_system' => 1,
                    'is_active' => 1,
                ],
                [
                    'user_id' => null,
                    'name' => '工作学习',
                    'icon' => '📚',
                    'color' => '#13c2c2',
                    'description' => '办公、学习、教育平台',
                    'sort' => 6,
                    'is_system' => 1,
                    'is_active' => 1,
                ],
            ];

            foreach ($systemCategories as $category) {
                $category['created_at'] = date('Y-m-d H:i:s');
                $category['updated_at'] = date('Y-m-d H:i:s');

                // 检查是否已存在
                $exists = Category::where('name', $category['name'])
                    ->where('is_system', 1)
                    ->find();

                if (!$exists) {
                    Category::create($category);
                    $output->writeln("已插入分类: {$category['name']}");
                } else {
                    $output->writeln("分类已存在: {$category['name']}");
                }
            }

            $output->writeln("系统分类初始化完成！");

        } catch (\Exception $e) {
            $output->writeln("错误: " . $e->getMessage());
        }
    }
}